{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Search and Geocode Tutorial\n",
    "This is the completed solution for the [Search and geocode](https://developers.arcgis.com/labs/develop/python/search-and-geocode/) ArcGIS tutorial. \n",
    "\n",
    "[ArcGIS tutorials](https://developers.arcgis.com/labs/) are short guides demonstrating the three phases of building geospatial apps: Data, Design, Develop."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from arcgis.gis import GIS\n",
    "from arcgis.geocoding import geocode, reverse_geocode\n",
    "from arcgis.geometry import Point"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "gis = GIS()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Geocode place names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{\"geometry\": {\"x\": -118.30054999999999, \"y\": 34.120760000000075, \"spatialReference\": {\"wkid\": 4326, \"latestWkid\": 4326}}, \"attributes\": {\"Loc_name\": \"World\", \"Status\": \"M\", \"Score\": 100, \"Match_addr\": \"Hollywood Sign\", \"LongLabel\": \"Hollywood Sign, 2800 E Observatory Rd, Los Angeles, CA, 90027, USA\", \"ShortLabel\": \"Hollywood Sign\", \"Addr_type\": \"POI\", \"Type\": \"Historical Monument\", \"PlaceName\": \"Hollywood Sign\", \"Place_addr\": \"2800 E Observatory Rd, Los Angeles, California, 90027\", \"Phone\": \"(213) 300-0108\", \"URL\": \"\", \"Rank\": 17, \"AddBldg\": \"\", \"AddNum\": \"2800\", \"AddNumFrom\": \"\", \"AddNumTo\": \"\", \"AddRange\": \"\", \"Side\": \"L\", \"StPreDir\": \"E\", \"StPreType\": \"\", \"StName\": \"Observatory\", \"StType\": \"Rd\", \"StDir\": \"\", \"BldgType\": \"\", \"BldgName\": \"\", \"LevelType\": \"\", \"LevelName\": \"\", \"UnitType\": \"\", \"UnitName\": \"\", \"SubAddr\": \"\", \"StAddr\": \"2800 E Observatory Rd\", \"Block\": \"\", \"Sector\": \"\", \"Nbrhd\": \"Los Feliz\", \"District\": \"\", \"City\": \"Los Angeles\", \"MetroArea\": \"Los Angeles Metro Area\", \"Subregion\": \"Los Angeles County\", \"Region\": \"California\", \"RegionAbbr\": \"CA\", \"Territory\": \"\", \"Zone\": \"\", \"Postal\": \"90027\", \"PostalExt\": \"\", \"Country\": \"USA\", \"LangCode\": \"ENG\", \"Distance\": 0, \"X\": -118.30054999999999, \"Y\": 34.120760000000075, \"DisplayX\": -118.30054999999999, \"DisplayY\": 34.120760000000075, \"Xmin\": -118.30554999999998, \"Xmax\": -118.29554999999999, \"Ymin\": 34.11576000000007, \"Ymax\": 34.12576000000008, \"ExInfo\": \"\", \"OBJECTID\": 1}}]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "geocode_result = geocode(address=\"Hollywood sign\", as_featureset=True)\n",
    "geocode_result.features"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "807eb4ce06914feaaa7702e37f55c815",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "MapView(layout=Layout(height='400px', width='100%'), zoom=11.0)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div class=\"map-static-img-preview-81937c4a-4cf9-4420-8bc7-15243192c6fc\"><img src=\"\"></img></div>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "map = gis.map(\"Los Angeles, CA\", zoomlevel=11)\n",
    "map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "map.draw(geocode_result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "map.clear_graphics()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Reverse geocode a coordinate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "location = {\n",
    "    'Y': 34.13419,\n",
    "    'X': -118.29636,\n",
    "    'spatialReference': {\n",
    "        'wkid':4326\n",
    "    }\n",
    "}\n",
    "\n",
    "unknown_pt = Point(location)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'address': {'Match_addr': 'Vista del Valle Dr, Los Angeles, California, 90027',\n",
       "  'LongLabel': 'Vista del Valle Dr, Los Angeles, CA, 90027, USA',\n",
       "  'ShortLabel': 'Vista del Valle Dr',\n",
       "  'Addr_type': 'StreetName',\n",
       "  'Type': '',\n",
       "  'PlaceName': '',\n",
       "  'AddNum': '',\n",
       "  'Address': 'Vista del Valle Dr',\n",
       "  'Block': '',\n",
       "  'Sector': '',\n",
       "  'Neighborhood': 'Los Feliz',\n",
       "  'District': '',\n",
       "  'City': 'Los Angeles',\n",
       "  'MetroArea': 'Los Angeles Metro Area',\n",
       "  'Subregion': 'Los Angeles County',\n",
       "  'Region': 'California',\n",
       "  'Territory': '',\n",
       "  'Postal': '90027',\n",
       "  'PostalExt': '',\n",
       "  'CountryCode': 'USA'},\n",
       " 'location': {'x': -118.29641724469197,\n",
       "  'y': 34.13381075391577,\n",
       "  'spatialReference': {'wkid': 4326, 'latestWkid': 4326}}}"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "address = reverse_geocode(unknown_pt)\n",
    "address"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "map.draw(address)"
   ]
  }
 ],
 "metadata": {
  "esriNotebookRuntime": {
   "notebookRuntimeName": "ArcGIS Notebook Python 3 Standard",
   "notebookRuntimeVersion": "9.0"
  },
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
